﻿using System;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace XamlingCore.AzureHelper
{
    public class AzureQueueHelper
    {
        CloudStorageAccount cloudStorageAccount;
        CloudQueueClient queue;
        CloudQueue queueReference;

        bool oneMessageOnly = false;

        public AzureQueueHelper(string queueAddress)
        {
            cloudStorageAccount = AzureCommon.GetStorageAccount();
            queue = cloudStorageAccount.CreateCloudQueueClient();
            
            queueReference = queue.GetQueueReference(queueAddress.ToLower());
            queueReference.CreateIfNotExist();
        }

        public AzureQueueHelper(string queueAddress, bool oneMessageOnly)
        {
            cloudStorageAccount = AzureCommon.GetStorageAccount();
            queue = cloudStorageAccount.CreateCloudQueueClient();

            queueReference = queue.GetQueueReference(queueAddress.ToLower());
            queueReference.CreateIfNotExist();
            this.oneMessageOnly = oneMessageOnly;
        }

        public void AddMessage(string messageContent)
        {
            if (oneMessageOnly)
            {
                if (GetMessage(false, TimeSpan.Zero)!=null)
                {
                    return;
                }
            }
            CloudQueueMessage message = new CloudQueueMessage(messageContent);            
            queueReference.AddMessage(message);
        }

        public void AddMessage(byte[] messageContent)
        {
            if (oneMessageOnly)
            {
                if (GetMessage(false, TimeSpan.Zero) != null)
                {
                    return;
                }
            }
            CloudQueueMessage message = new CloudQueueMessage(messageContent);
            queueReference.AddMessage(message);
        }

        public CloudQueueMessage GetMessage(bool delete, TimeSpan delay)
        {
            if (queueReference.Exists())
            {
                CloudQueueMessage result;
                if (delay == TimeSpan.Zero)
                {
                    result = queueReference.GetMessage();
                }
                else
                {
                    result = queueReference.GetMessage(delay);
                }
                
                if (result != null && delete)
                {
                    queueReference.DeleteMessage(result);
                }
                return result;
            }
            else
            {
                return null;
            }
        }

        public bool HasMessages()
        {
            return GetMessage(false, TimeSpan.Zero) != null;
        }

        public CloudQueueMessage GetMessage(bool delete)
        {            
            if (queueReference.Exists())
            {
                var result = queueReference.GetMessage(TimeSpan.FromSeconds(120));
                
                if (result != null && delete)
                {
                    queueReference.DeleteMessage(result);
                }
                
                return result;
            }
            else
            {
                return null;
            }
        }

        public void DeleteQueueMessage(CloudQueueMessage message)
        {
            try
            {
                queueReference.DeleteMessage(message);
            }
            catch { }
        }
    }
}
